package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@SuppressWarnings("all")
@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;


    @Transactional
    @Override
    public void saveSetmeal(SetmealDTO setmealDTO) {
        //创建setmeal对象
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        System.out.println(setmeal);
        setmealMapper.insert(setmeal);
        System.out.println(setmeal.getId());

        //获取套餐下菜品
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        System.out.println(setmealDishes);
        setmealDishes.forEach(setmealDish -> {
            //插入套餐后能够获取到套餐id，这个套餐id作为外键，插入套餐菜品表
            setmealDish.setSetmealId(setmeal.getId());
            //在循环内部插入，可以一条信息插入，不必写动态sql
            setmealDishMapper.insert(setmealDish);
        });
    }

    @Override
    //分页查询
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        //使用分页插件
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());

        //查询数据
        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);

        //封装数据,总条数，分页具体数据
        return new PageResult(page.getTotal(),page.getResult());


    }
    @Transactional
    @Override
    public SetmealDTO getById(Long id) {
        //根据id查询套餐基本信息
        Setmeal setmeal = setmealMapper.getById(id);
        SetmealDTO setmealDTO = new SetmealDTO();
        BeanUtils.copyProperties(setmeal,setmealDTO);
        //根据id查询套餐关联的菜品信息
        List<SetmealDish> setmealDishes = setmealDishMapper.getBySetmealId(id);
        //封装数据
        setmealDTO.setSetmealDishes(setmealDishes);
        return setmealDTO;
    }
    @Transactional
    @Override
    public void update(SetmealDTO setmealDTO) {
        //创建setmeal对象、
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);

        //修改setmeal表内容
        setmealMapper.update(setmeal);

        //根据当前id，删除setmeal_dish表内容
        Long setmeal_id = setmealDTO.getId();
        setmealDishMapper.deleteBySetmealId(setmeal_id);

        //重新插入setmeal_dish表内容
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmeal_id);
            setmealDishMapper.insert(setmealDish);
        });


    }

    @Override
    public void updateStatus(Integer status, Long id) {
        //调用修改的动态sql
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        setmeal.setId(id);

        setmealMapper.update(setmeal);
    }
    @Transactional
    //删除套餐，同时删除套餐和菜品的关联数据
    @Override
    public void delete(List<Long> ids) {
        //查询套餐
        for (Long id : ids) {
            Setmeal setmeals = setmealMapper.getById(id);
            if (setmeals.getStatus()== StatusConstant.ENABLE){
                //如果当前套餐正在售卖中，抛出一个业务异常
                throw new RuntimeException("套餐正在售卖中，不能删除");
            }
        }
        //删除套餐表中的数据
        setmealMapper.delete(ids);
        //删除套餐和菜品的关联数据
        for (Long id : ids) {
            setmealDishMapper.deleteBySetmealId(id);
        }


    }

    @Override
    //根据分类id查询套餐
    public List<Setmeal> userList(Long categoryId) {
        //获取分类id下的所有套餐信息
        List<Setmeal> stealList = setmealMapper.userList(categoryId);

        /**
        //创建一个vo对象
        List<SetmealVO> setmealVOS = new ArrayList<>();
        //遍历套餐信息，获取套餐信息下所有的菜品信息，根据id查询菜品信息
        for (Setmeal setmeal : stealList) {
            //创建vo对象
            SetmealVO setmealVO = new SetmealVO();
            //根据id查询菜品信息
            List<SetmealDish> setmealDishes = setmealDishService.getBySetmealId(setmeal.getId());
            //封装数据，将套餐信息和菜品信息封装到vo对象中
            BeanUtils.copyProperties(setmeal,setmealVO);
            setmealVO.setSetmealDishes(setmealDishes);
            //将vo对象添加到list集合中
            setmealVOS.add(setmealVO);
        }
         */

        return stealList;
    }

    @Override
    public List<DishItemVO> userListDish(Long id) {
        //获取套餐下的所有菜品信息
        List<DishItemVO> dishList = setmealDishMapper.userListDish(id);
        return dishList;
    }
}
